package com.gln.bloger.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.gln.bloger.dao.TagDao;
import com.gln.bloger.exception.BlogerException;
import com.gln.bloger.pojos.Tag;
import com.gln.bloger.pojos.Website;

/**
 * 
 * @author renxj
 *
 */
public class TagDaoImpl extends BaseDaoImpl<Tag> implements TagDao{

	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<Tag> getHotTags(Website website, int size) throws BlogerException {
		DetachedCriteria criteria = DetachedCriteria.forClass(Tag.class);
		criteria.add(Restrictions.eq("website", website));
		criteria.addOrder(Order.desc("count"));
		List<Tag> tags = getHibernateTemplate().findByCriteria(criteria, -1, size);
		return tags;
	}

	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<Tag> list(Website website) throws BlogerException {
		DetachedCriteria criteria = DetachedCriteria.forClass(Tag.class);
		criteria.add(Restrictions.eq("website", website));
		criteria.addOrder(Order.desc("count"));
		List<Tag> tags = getHibernateTemplate().findByCriteria(criteria);
		return tags;
	}

	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Tag getTagByName(Website website, String name)
			throws BlogerException {
		DetachedCriteria criteria = DetachedCriteria.forClass(Tag.class);
		criteria.add(Restrictions.eq("website", website));
		criteria.add(Restrictions.eq("name", name));
		criteria.addOrder(Order.desc("count"));
		List<Tag> tags = getHibernateTemplate().findByCriteria(criteria);
		return tags.get(0);
	}
}
